package com.kanshu.ksgb.fastread.doudou.module.message.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dl7.recycler.adapter.BaseMultiItemQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.message.utils.MessageUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.MessageBean;

import java.util.List;

import static com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils.formatServerTime;
import static com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils.processJump;

/**
 * Created by wcy on 2017\6\26 0026.
 */

public class MessageAdapter extends BaseMultiItemQuickAdapter<MessageBean> {
    private GlideImageConfig mConfig = GlideImageLoader.getEmptyConfig();
    public MessageAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getDefaultLayoutId() {
        return R.layout.item_msg_type_one_layout;
    }

    public MessageAdapter(Context context, List<MessageBean> data) {
        super(context, data);
    }
    @Override
    protected void attachItemType() {
        addItemType(MessageBean.TYPE_MESSAGE_ONE, R.layout.item_msg_type_one_layout);
        // type_int 为2也是显示类型1的数据
        addItemType(MessageBean.TYPE_MESSAGE_TWO, R.layout.item_msg_type_one_layout);
        addItemType(MessageBean.TYPE_MESSAGE_THREE, R.layout.item_msg_type_three_layout);
    }

    @Override
    protected void convert(BaseViewHolder holder, MessageBean item, int position) {
        switch (item.getItemType()) {
            case MessageBean.TYPE_MESSAGE_ONE:
                handleTypeOne(holder, position, item);
                break;
            case MessageBean.TYPE_MESSAGE_THREE:
                handleTypeThree(holder, position, item);
                break;
            case MessageBean.TYPE_MESSAGE_TWO:
                handleTypeOne(holder, position, item);
                break;
            default:
                // 默认布局处理
                handleTypeOne(holder, position, item);
                break;
        }
    }

    private void handleMsgTime(int pos, TextView msgTime) {
        MessageBean msg1 = (MessageBean) mData.get(pos);
        msgTime.setText(formatServerTime(msg1.createtime, "yyyy年M月d日 HH:mm"));
        if (pos == 0) {
            DisplayUtils.visible(msgTime);
        } else {
            MessageBean msg2 = (MessageBean) mData.get(pos - 1);
            String s1 = BookUtils.formatServerTime(msg1.createtime, "yyyy年M月d日 HH:mm");
            String s2 = BookUtils.formatServerTime(msg2.createtime, "yyyy年M月d日 HH:mm");
            Log.d("wcy", "msg1:" + s1);
            Log.d("wcy", "msg2:" + s2);
            if (Utils.isSameDay(msg1.createtime, msg2.createtime)) {
                DisplayUtils.gone(msgTime);
            } else {
                DisplayUtils.visible(msgTime);
            }
        }
    }
    private void handleTypeOne(BaseViewHolder holder, int position, final MessageBean item) {
        handleMsgTime(position, (TextView) holder.getView(R.id.msg_time));
        final ImageView imageView = holder.getView(R.id.big_img);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                processJump(mContext, Integer.parseInt(item.link_type), item.url);
                MessageUtils.countMessageClick(item.message_id, 0);
            }
        });
        TextView tv = holder.getView(R.id.big_desc);
        LinearLayout listContainer = holder.getView(R.id.list_container);
        GlideImageLoader.load(item.img_url, imageView, mConfig);
        tv.setText(item.title);
        if (Utils.isEmptyList(item.messages)) {
            listContainer.setVisibility(View.GONE);
        } else {
            TextView tv1 = holder.getView(R.id.one_msg_txt);
            TextView tv2 = holder.getView(R.id.two_msg_txt);
            TextView tv3 = holder.getView(R.id.three_msg_txt);
            TextView tv4 = holder.getView(R.id.four_msg_txt);
            TextView tv5 = holder.getView(R.id.five_msg_txt);
            ImageView iv1 = holder.getView(R.id.one_msg_img);
            ImageView iv2 = holder.getView(R.id.two_msg_img);
            ImageView iv3 = holder.getView(R.id.three_msg_img);
            ImageView iv4 = holder.getView(R.id.four_msg_img);
            ImageView iv5 = holder.getView(R.id.five_msg_img);
            listContainer.setVisibility(View.VISIBLE);
            int size = item.messages.size() > 5 ? 5 : item.messages.size();
            if (item.messages.size() > 5) {
                item.messages.subList(0, 5);
            }
            // 处理跳转和显示隐藏
            for (int i = 0; i < listContainer.getChildCount(); i++) {
                if (i < size) {
                    listContainer.getChildAt(i).setVisibility(View.VISIBLE);
                    final MessageBean.SubMessageBean subItem = item.messages.get(i);
                    final int pos = i + 1;
                    listContainer.getChildAt(i).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            processJump(mContext, Integer.parseInt(subItem.link_type), subItem.url);
                            MessageUtils.countMessageClick(item.message_id, pos);
                        }
                    });
                } else {
                    listContainer.getChildAt(i).setVisibility(View.GONE);
                }
            }
            // 显示图片文字
            for (int i = 0; i < item.messages.size(); i++) {
                switch (i) {
                    case 0:
                        tv1.setText(item.messages.get(i).title);
                        GlideImageLoader.load(item.messages.get(i).img_url, iv1, mConfig);
                        break;
                    case 1:
                        tv2.setText(item.messages.get(i).title);
                        GlideImageLoader.load(item.messages.get(i).img_url, iv2, mConfig);
                        break;
                    case 2:
                        tv3.setText(item.messages.get(i).title);
                        GlideImageLoader.load(item.messages.get(i).img_url, iv3, mConfig);
                        break;
                    case 3:
                        tv4.setText(item.messages.get(i).title);
                        GlideImageLoader.load(item.messages.get(i).img_url, iv4, mConfig);
                        break;
                    case 4:
                        tv5.setText(item.messages.get(i).title);
                        GlideImageLoader.load(item.messages.get(i).img_url, iv5, mConfig);
                        break;
                    default:
                        break;
                }
            }
        }

    }

    private void handleTypeThree(BaseViewHolder holder, int position, MessageBean item) {
        handleMsgTime(position, (TextView) holder.getView(R.id.msg_time));
        TextView tv = holder.getView(R.id.chat_msg);
        ImageView imageView = holder.getView(R.id.one_msg_img);
        tv.setText(item.title);
        GlideImageLoader.load(item.img_url, imageView, GlideImageLoader.getEmptyConfig());
    }

    private void handleTypeTwo(BaseViewHolder holder, int position, MessageBean item) {
        handleMsgTime(position, (TextView) holder.getView(R.id.msg_time));
        TextView tv = holder.getView(R.id.one_msg_txt);
        ImageView imageView = holder.getView(R.id.one_msg_img);
        tv.setText(item.title);
        GlideImageLoader.load(item.img_url, imageView, GlideImageLoader.getEmptyConfig());
    }



}
